/**
 * 
 */
package com.wsd.action.admin;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.BeanUtils;

import com.wsd.action.BaseAction;
import com.wsd.bean.Users;
import com.wsd.service.UserService;

/**
 * @author Administrator
 *
 */
public class UserAction extends BaseAction {
	
	private UserService userService;
	private Users user;
	private String text;
	private boolean search;
	private int start;
	private int total;
	private int limit;
	private int userLevel;
	private int showType;
	private String key;
	private int[] userIds;
	private boolean state;
	@JSON(serialize = false)
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@JSON(serialize = false)

	public Users getUser() {
		return user;
	}
	public void setUser(Users user) {
		this.user = user;
	}
	@JSON(serialize = false)
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	@JSON(serialize = false)
	public boolean isSearch() {
		return search;
	}
	public void setSearch(boolean search) {
		this.search = search;
	}
	
	@JSON(serialize = false)
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	@JSON(serialize = false)
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	@JSON(serialize = false)
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	@JSON(serialize = false)
	public int getUserLevel() {
		return userLevel;
	}
	public void setUserLevel(int userLevel) {
		this.userLevel = userLevel;
	}
	@JSON(serialize = false)
	public int getShowType() {
		return showType;
	}
	public void setShowType(int showType) {
		this.showType = showType;
	}
	@JSON(serialize = false)
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	@JSON(serialize = false)
	public int[] getUserIds() {
		return userIds;
	}
	public void setUserIds(int[] userIds) {
		this.userIds = userIds;
	}
	@JSON(serialize = false)
	public boolean isState() {
		return state;
	}
	public void setState(boolean state) {
		this.state = state;
	}
	/**
	 * 添加用户
	 * @return
	 */
	public String add(){
		//测试用户名是否已被注册
		if(userService.checkUserName(user.getUsername())){
			result.put("msg", "已经存在的用户名");
			success = false;
		}else{
			//默认密码都为123456
			user.setUserpass("123456");
			user.setRegisterTime(new Timestamp(new Date().getTime()));
			user.setState(true);//后台添加用户都为已激活
			userService.saveUser(user);
		}
		
		return AJAX;
	}
	
	public String search(){
		List<Users> users = null;
		List<String> pNames = new ArrayList<String>();
		List<Object> pValues = new ArrayList<Object>();
		buildParams(user,pNames,pValues);
		if(search){
			users = userService.searchUsers(key,pNames.toArray(new String[pNames.size()]),pValues.toArray(),start,limit);
			total = userService.searchUsersCount(key,pNames.toArray(new String[pNames.size()]),pValues.toArray());

		}else{
			if(userLevel != 0){
				user.setUserLevel(userLevel);
			}else{
				user.setUserLevel(null);
			}
			users = userService.findUsers(pNames.toArray(new String[pNames.size()]),pValues.toArray(),start,limit);
			total = userService.findUsersCount(pNames.toArray(new String[pNames.size()]),pValues.toArray());
		}
		if(user != null){
			process(users);
		}
		result.put("users", users);
		return AJAX;
	}
	
	/**
	 * @param users
	 */
	private void process(List<Users> users) {
		for(Users user : users){
			user.setMessages(null);
			user.setProductOrders(null);
		}
	}
	public String updateState(){
		userService.updateState(userIds,state);
		return AJAX;
	}
	public String updateLevel(){
		userService.updateLevel(userIds,userLevel);
		return AJAX;
	}
	
	public String updateUser(){
		Users preUser = userService.findById(user.getUserid());
		BeanUtils.copyProperties(user, preUser, new String[]{"productOrders","messages","registerTime","userpass"});
		userService.updateUser(preUser);
		return AJAX;
	}
	
	/**
	 * @param user2
	 * @param names
	 * @param values
	 */
	private void buildParams(Users user, List<String> names, List<Object> values) {
		if(user.getUserLevel() != 0){
			names.add("userLevel");
			values.add(user.getUserLevel());
		}
		if(showType == 2){
			names.add("state");
			values.add(true);
		}else if(showType == 3){
			names.add("state");
			values.add(false);
		}
	}
}
